//
//  WordsModel.swift
//  Keyboard
//
//  Created by 周张林 on 17/8/19.
//  Copyright © 2017年 周张林. All rights reserved.
//

import Foundation

class WordsModel:NSObject,XMLParserDelegate {
    private static var _instance:WordsModel!
    public static var instance:WordsModel!{
        get{
            if _instance == nil {
                _instance = WordsModel()
            }
            return _instance
        }
    }
    private override init(){
        _wordsKV = [String:[WordEntity]]()
        for c in DefaultKeyboardWordPad.wordArray {
            _wordsKV[c] = [WordEntity]()
        }
        
        _allWords = [WordEntity]()
        super.init()
    }
    
    private var _wordsKV:[String:[WordEntity]]
    private var _allWords:[WordEntity]
    private var _currF:String = "0";
    
    func loadWords(){
        let parser = XMLParser(contentsOf:URL.init(fileURLWithPath: Bundle.main.path(forResource: "words", ofType: "xml")!))
        parser!.delegate = self
        parser!.parse()
    }
    
    
    public func parser(_ parser: XMLParser, foundCharacters string: String){
        
        let firstChar = String.init(string.characters[string.startIndex]).lowercased()
        if var words = _wordsKV[firstChar]{
            let entity = WordEntity()
            entity.word = string
            entity.f = Int.init(_currF)!
            words.append(entity)
            
            _wordsKV[firstChar] = words
            _allWords.append(entity)
        }
    }
    
    public func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:])
    {
        if elementName == "w" {
            _currF = attributeDict["f"]!
        }
    }
    
    func searchWords(pre:String!)->[String]
    {
        var result = [String]()
        let firstChar = String.init(pre.characters[pre.startIndex]).lowercased()
        if let words = _wordsKV[firstChar]{
            for w in words{
                let range = w.word.range(of: w.word)
                if w.word != pre && w.word.range(of: pre, options: .caseInsensitive, range: range, locale: nil)?.isEmpty == false
                {
                    result.append(w.word)
                }
            }
        }
        return result
    }
    
    /// 联想
    ///
    /// - Parameter pre: 之前的词
    /// - Returns: 联想后的词
    func associate(pre:String!)->[String]?
    {
        let word = pre.lowercased()
        let firstChar = String.init(word.characters[word.startIndex])
        if let words = _wordsKV[firstChar]{
            for w in words{
                if w.word.lowercased() == word {
                   return getWordsByF(w.f)
                }
            }
        }
        return nil
    }
    
    func getWordsByF(_ f:Int)->[String]
    {
        var results = [String]()
        for entity in _allWords{
            if entity.f == f {
                results.append(entity.word)
            }
        }
        return results
    }
}
